package com.project.service.impl;

import com.project.bean.StudentBean;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class StudentImplService extends BaseService implements IStudentService {
    @Override
    public int add(StudentBean student) {
        SqlSession session = this.getSession();
        //使用代理方式得到Mapper接口实现类对象
       IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.add(student);
        session.commit();
        System.out.println(student);
        session.close();
        return student.getId();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
       IStudentMapper mapper = session.getMapper(IStudentMapper.class);
       mapper.del(id);
       session.commit();
       session.close();
    }

    @Override
    public void delMore(int[] idArray) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.delMore(idArray);
        session.commit();
        session.close();
    }

    @Override
    public void update(int id, String tel) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.update(id,tel);
        session.commit();
        session.close();
    }

    @Override
    public List<StudentBean> findAll() {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> list = mapper.findAll();
        session.commit();
        session.close();
        return list;
    }

    @Override
    public StudentBean findById(int id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean stu = mapper.findById(id);
        session.commit();
        session.close();
        return stu;
    }

    @Override
    public List<StudentBean> findByName(String name) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> list = mapper.findByName("%"+name+"%");
        session.commit();
        session.close();
        return list;
    }
    public static void main(String[] args){
        IStudentService studentService = new StudentImplService();
//        studentService.add(new StudentBean("bbb","18888888888", LocalDate.parse("1998-05-26")));
//        studentService.del(14);
//        studentService.update(13,"13000000000");
//        studentService.delMore(new int[]{9,11});
//        System.out.println(studentService.findAll());
//          System.out.println(studentService.findById(5));
        System.out.println(studentService.findByName("刘"));
    }
}
